const router = require('koa-router')()
const JWT = require('jsonwebtoken')
const key = 'eiwuhfdqowlq'
const userList = [{
  "username": 'admin',
  "password": "admin",
  "nickname": "管理员用户",
  "avatar": 'https://robohash.org/123?set=set5'
}, {
  "username": 'test',
  "password": "test",
  "nickname": "测试用户",
  "avatar": 'https://robohash.org/8765?set=set5'
}]

router.post('/login', ctx => {
  const { username, password } = ctx.request.body;
  const user = userList.find(item => item.username === username && item.password === password);
  if (user) {
    ctx.body = {
      code: 200,
      message: '登录成功',
      data: {
        userInfo: user,
        token: JWT.sign(user, key)
      }
    }
  } else {
    ctx.body = {
      code: 400,
      message: '用户名或密码错误'
    }
  }
})
module.exports = router
